﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BusinessLogic;

namespace Interface.Houses
{
    public partial class BoardingHouse : System.Web.UI.Page
    {
        DataValid dv = new DataValid();
        DataConnection dc = new DataConnection();
        
 
         
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["OwnerSession"] != null)
            {
                btnThem.Visible = true;
                btnBack.Visible = true;
            }


            if (Request.QueryString["did"] == null && Request.QueryString["CityID"] == null && Request.QueryString["DisID"] == null && Request.QueryString["CateID"] == null && Request.QueryString["Min"] == null && Request.QueryString["Max"] == null)
            {
                bindToGrid();
                btnBack.Visible = false;
            }
            else if (Request.QueryString["did"] != null && Request.QueryString["CityID"] == null && Request.QueryString["DisID"] == null && Request.QueryString["CateID"] == null && Request.QueryString["Min"] == null && Request.QueryString["Max"] == null)
            {
                string stringidDis;               
                stringidDis = Request.QueryString["did"].ToString();
                searchdis(stringidDis);
            }
            else if (Request.QueryString["did"] == null && Request.QueryString["CityID"] != null && Request.QueryString["DisID"] == null && Request.QueryString["CateID"] == null && Request.QueryString["Min"] == null && Request.QueryString["Max"] == null)
            {
                string stringidDis;
                stringidDis = Request.QueryString["CityID"].ToString();
                searchCity(stringidDis);
            }
            else if (Request.QueryString["did"] == null && Request.QueryString["CityID"] != null && Request.QueryString["DisID"] != null && Request.QueryString["CateID"] == null && Request.QueryString["Min"] == null && Request.QueryString["Max"] == null)
            {
                string CityID = Request.QueryString["CityID"].ToString();
                string DisID = Request.QueryString["DisID"].ToString();
                searchCityDis(CityID , DisID);
 
            }
            else if (Request.QueryString["did"] == null && Request.QueryString["CityID"] != null && Request.QueryString["DisID"] != null && Request.QueryString["CateID"] != null && Request.QueryString["Min"] == null && Request.QueryString["Max"] == null)
            {
                string CityID = Request.QueryString["CityID"].ToString();
                string DisID = Request.QueryString["DisID"].ToString();
                string CateID = Request.QueryString["CateID"].ToString();
                searchCityDisCate(CityID, DisID,CateID);

            }

            else if (Request.QueryString["did"] == null && Request.QueryString["CityID"] != null && Request.QueryString["DisID"] != null && Request.QueryString["CateID"] != null && Request.QueryString["Min"] == null && Request.QueryString["Max"] == null)
            {
                lbErrors.Text = "Không có nhà như bạn yêu cầu";
            }

            else if (Request.QueryString["did"] == null && Request.QueryString["CityID"] != null && Request.QueryString["DisID"] != null && Request.QueryString["CateID"] != null && Request.QueryString["Min"] != null && Request.QueryString["Max"] != null)
            {
                string CityID = Request.QueryString["CityID"].ToString();
                string DisID = Request.QueryString["DisID"].ToString();
                string CateID = Request.QueryString["CateID"].ToString();
                int Min = Convert.ToInt32(Request.QueryString["Min"]);
                int Max = Convert.ToInt32(Request.QueryString["Max"]);
                //searchAll(CityID, DisID,CateID, Min, Max);

            }
        }

        //private void searchAll(string CityID, string DisID, string CateID, int Min, int Max)
        //{
        //    string sqlPrice = null;
        //    if (Max == 500000)
        //    {
        //        sqlPrice = "Select HouseID from Rooms where Price<'" + Max + "'";
        //    }
        //    if (Min == 500000 && Max == 1000000)
        //    {
        //        sqlPrice = "Select HouseID from Rooms where Price<'" + Max + "' and Price >'" + Min + "'";
        //    }
        //    if (Min == 1000000)
        //    {
        //        sqlPrice = "Select HouseID from Rooms where Price >'" + 1000000 + "'";

        //    }

        //    DataTable dtRooms = dc.getData(sqlPrice);
        //    DataTable newdt = dataTableInitiate();
        //    DataRow m = null;
        //    if (dtRooms.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < dtRooms.Rows.Count; i++)
        //        {
        //            DataRow row = dtRooms.Rows[i];
        //            int IdHouse = Convert.ToInt32(row["HouseID"]);
        //            string sqlAll = "select * from BoardingHouse where City = '" + CityID + "'and District = '" + DisID + "' and Category = '" + CateID + "' and IdHouse= '" + IdHouse + "'";
        //            m = NewRow(sqlAll);
        //            newdt.Rows.Add(m);
        //        }


        //    }

        //    gridHouse.DataSource = newdt;
        //    gridHouse.DataBind();
        //}

        //private DataRow NewRow(string sql)
        //{
        //    DataTable dt = dc.getData(sql);
        //    DataTable dtload = dataTableInitiate();
        //    DataRow n = null;
        //    if (dt.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            int stt = i + 1;
        //            DataRow row = dt.Rows[i];
        //            int ID = Convert.ToInt32(row["IdHouse"].ToString());

        //            int _cityId = Convert.ToInt32(row["City"]);

        //            string _cityName = dc.getCityName(_cityId);

        //            int _disId = Convert.ToInt32(row["District"]);

        //            string _disName = dc.getDisName(_disId);

        //            string _street = Convert.ToString(row["Street"]);

        //            string _ward = Convert.ToString(row["Ward"]);

        //            string _diachi = " Đường " + _street + ", phường " + _ward + ", quận " + _disName;

        //            int _loainhatro = Convert.ToInt32(row["Category"]);

        //            string _tenLoainhatro = dc.getCategoryName(_loainhatro);

        //            string _hinh = row["ImageSource"].ToString();

        //            string _lienhe = row["Contact"].ToString();

        //            DataRow dtRow = dtload.NewRow();
        //            dtRow["ID"] = ID;
        //            dtRow["STT"] = stt;
        //            dtRow["ThanhPho"] = _cityName;

        //            dtRow["DiaChi"] = _diachi;
        //            dtRow["LoaiNhaTro"] = _tenLoainhatro;

        //            dtRow["Hinh"] = "~/Images/Houses/" + _hinh;
        //            dtRow["LienHe"] = _lienhe;

        //            n = dtRow;
        //        }
        //    }
        //    return n;
        //}

        private void searchCityDisCate(string CityID, string DisID, string CateID)
        {
            string sqlsearch = "select * from BoardingHouse where City = '" + CityID + "'and District = '" + DisID + "' and Category = '"+ CateID +"' ";
            loaddata(sqlsearch);
        }

        private void searchCityDis(string CityID, string DisID)
        {
            string sqlsearch = "select * from BoardingHouse where City = '" + CityID + "'and District = '"+ DisID +"' ";
            loaddata(sqlsearch);
        }

        private void searchCity(string stringidCity)
        {
            string sqlsearch = "select * from BoardingHouse where City = '" + stringidCity + "' ";
            loaddata(sqlsearch);
        }

        private void searchdis(string id)
        {
            string sqlsearch = "select * from BoardingHouse where District = '" + id + "' ";
            loaddata(sqlsearch);

            
        }

        private void loaddata(string sql)
        {
            DataTable dt = dc.getData(sql);
            DataTable dtload = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["IdHouse"].ToString());

                    int _cityId = Convert.ToInt32(row["City"]);

                    string _cityName = dc.getCityName(_cityId);

                    int _disId = Convert.ToInt32(row["District"]);

                    string _disName = dc.getDisName(_disId);

                    string _street = Convert.ToString(row["Street"]);

                    string _ward = Convert.ToString(row["Ward"]);

                    string _diachi = " Đường " + _street + ", phường " + _ward + ", quận " + _disName;

                    int _loainhatro = Convert.ToInt32(row["Category"]);

                    string _tenLoainhatro = dc.getCategoryName(_loainhatro);

                    string _hinh = row["ImageSource"].ToString();

                    string _lienhe = row["Contact"].ToString();

                    DataRow dtRow = dtload.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["STT"] = stt;
                    dtRow["ThanhPho"] = _cityName;

                    dtRow["DiaChi"] = _diachi;
                    dtRow["LoaiNhaTro"] = _tenLoainhatro;

                    dtRow["Hinh"] = "~/Images/Houses/" + _hinh;
                    dtRow["LienHe"] = _lienhe;

                    dtload.Rows.Add(dtRow);
                }
            }
            else
            {
                lbErrors.Text = "Không tìm thấy vui lòng thử từ khóa khác";
            }
            

            gridHouse.DataSource = dtload;
            gridHouse.DataBind();
 
        }

        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;
            dt.Columns.Add("STT", typeof(string));
            dt.Columns.Add("DiaChi", typeof(string));
            dt.Columns.Add("ThanhPho", typeof(string));
            dt.Columns.Add("Quan", typeof(string));
            dt.Columns.Add("LoaiNhaTro", typeof(string));
            //dt.Columns.Add("DanhGia", typeof(string));
            dt.Columns.Add("LienHe", typeof(string));
            dt.Columns.Add("Hinh", typeof(string));
            return dt;
        }


        protected void bindToGrid()
        {

            string strsql = "Select * from BoardingHouse order by IdHouse DESC";
            DataTable dt = dc.getData(strsql);
            DataTable dtUser = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["IdHouse"].ToString());

                    int _cityId = Convert.ToInt32(row["City"]);

                    string _cityName = dc.getCityName(_cityId);

                    int _disId = Convert.ToInt32(row["District"]);

                    string _disName = dc.getDisName(_disId);

                    string _street = Convert.ToString(row["Street"]);
                   
                    string _ward = Convert.ToString(row["Ward"]);

                    string _diachi = " Đường " + _street + ", phường " + _ward + ", quận " + _disName;

                    int _loainhatro = Convert.ToInt32(row["Category"]);

                    string _tenLoainhatro = dc.getCategoryName(_loainhatro);

                    //int _danhgia = Convert.ToInt32(row["Rating"]);
                    //string _dgia = "";

                    //if (_danhgia > 0)
                    //{
                    //    _dgia = "Loại: " + _danhgia;

                    //}
                    //else
                    //{
                    //    _dgia = "Chưa đánh giá";
                    //}

                    string _hinh = row["ImageSource"].ToString();

                    string _lienhe = row["Contact"].ToString();

                    DataRow dtRow = dtUser.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["STT"] = stt;
                    dtRow["ThanhPho"] = _cityName;
                    //dtRow["Quan"] = _disName;
                    dtRow["DiaChi"] = _diachi;
                    dtRow["LoaiNhaTro"] = _tenLoainhatro;
                    //dtRow["DanhGia"] = _dgia;
                    dtRow["Hinh"] = "~/Images/Houses/"+_hinh;
                    dtRow["LienHe"] = _lienhe;

                    dtUser.Rows.Add(dtRow);
                }
            }


            gridHouse.DataSource = dtUser;
            gridHouse.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            
            gridHouse.PageIndexChanging += new GridViewPageEventHandler(gridHouse_PageIndexChanging);
           

            gridHouse.SelectedIndexChanging += new GridViewSelectEventHandler(gridHouse_SelectedIndexChanging);
        }

        void gridHouse_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int id = (int)gridHouse.DataKeys[e.NewSelectedIndex].Value;
            Response.Redirect("HouseDetail.aspx?ID=" + id);
        }

        void gridHouse_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridHouse.PageIndex = e.NewPageIndex;
            bindToGrid();
        }

        

        protected void btnThem_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddHouse.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Search.aspx");
        }
    }
}